﻿/**
*    @file:    progressmanagedialog.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-01-03  18:29
*/

#include "progressmanagedialog.h"
#include "ui_progressmanagedialog.h"

#include "progresssavedialog.h"
#include <QDebug>
#include <QMenu>
#include <QMessageBox>
#include <delegatecenter.h>

ProgressManageDialog::ProgressManageDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ProgressManageDialog)
{
    ui->setupUi(this);
    tableModel = new QStandardItemModel();
    ui->tableView->setModel(tableModel);

    // 实现数据改变自动刷新的功能
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //单行选中
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);  //设置单元格不可编辑

    // 设置表头数据
    QStringList headers;
    headers << "id"
            << "程序地址"
            << "程序名称"
            << "消失拉起守护"
            << "CPU占用守护"
            << "CPU占用门限"
            << "心跳守护"
            << "心跳守护标识信息"
            << "心跳超时秒数"
            << "主动退出"
            << "初启拉起"
            << "启动等待"
            << "守护开关"
            << "创建时间"
            << "更新时间";
    tableModel->setHorizontalHeaderLabels(headers);

    ui->tableView->setColumnHidden(0, true);
    ui->tableView->setColumnHidden(1, true);
    ui->tableView->setColumnHidden(5, true);
    ui->tableView->setColumnHidden(7, true);
    ui->tableView->setColumnHidden(9, true);
    ui->tableView->setColumnHidden(11, true);
    ui->tableView->setColumnHidden(13, true);
    ui->tableView->setColumnHidden(14, true);

    //ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //==========================枚举值代理
    QStringList enumList1{QString("关闭"), QString("开启")};
    QStringList enumList2{QString("否"), QString("是")};

    EnumDelegate *enumDelegate1 = new EnumDelegate(enumList1, this);
    EnumDelegate *enumDelegate2 = new EnumDelegate(enumList2, this);

    ui->tableView->setItemDelegateForColumn(3, enumDelegate1);
    ui->tableView->setItemDelegateForColumn(4, enumDelegate1);
    ui->tableView->setItemDelegateForColumn(6, enumDelegate1);
    ui->tableView->setItemDelegateForColumn(9, enumDelegate2);
    ui->tableView->setItemDelegateForColumn(10, enumDelegate1);
    ui->tableView->setItemDelegateForColumn(12, enumDelegate1);

    selectAll();
}

ProgressManageDialog::~ProgressManageDialog()
{
    delete ui;
}

void ProgressManageDialog::addProgress()
{
    Progress progressInfo;
    ProgressSaveDialog *saveDialog = new ProgressSaveDialog(&progressInfo, this);
    saveDialog->setWindowIcon(QIcon(":/res/img/ico/add.ico"));
    if (saveDialog->exec() == QDialog::Accepted)
    {
        progressService.saveOne(progressInfo);
        selectAll();
    }
}

void ProgressManageDialog::upProgress()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();

    Progress progressInfo;
    progressInfo.setId(ui->tableView->model()->index(iSel, 0).data().toInt());
    progressInfo.setPath(ui->tableView->model()->index(iSel, 1).data().toString());
    progressInfo.setName(ui->tableView->model()->index(iSel, 2).data().toString());
    progressInfo.setVanish_up(ui->tableView->model()->index(iSel, 3).data().toBool());
    progressInfo.setCpu_use_up(ui->tableView->model()->index(iSel, 4).data().toBool());
    progressInfo.setCpu_level(ui->tableView->model()->index(iSel, 5).data().toInt());
    progressInfo.setHeartbeat_up(ui->tableView->model()->index(iSel, 6).data().toBool());
    progressInfo.setHeartbeat_msg(ui->tableView->model()->index(iSel, 7).data().toString());
    progressInfo.setHeartbeat_timeout_second(ui->tableView->model()->index(iSel, 8).data().toInt());
    progressInfo.setInit_start(ui->tableView->model()->index(iSel, 10).data().toBool());
    progressInfo.setStart_sleep(ui->tableView->model()->index(iSel, 11).data().toInt());
    progressInfo.setDaemon_open(ui->tableView->model()->index(iSel, 12).data().toBool());
    ProgressSaveDialog *saveDialog = new ProgressSaveDialog(&progressInfo, this);
    saveDialog->setWindowIcon(QIcon(":/res/img/ico/edit.ico"));
    if (saveDialog->exec() == QDialog::Accepted)
    {
        progressService.saveOne(progressInfo);
        selectAll();
    }
}

void ProgressManageDialog::delProgress()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();

    Progress progressInfo;
    progressInfo.setId(ui->tableView->model()->index(iSel, 0).data().toInt());
    progressService.deleteOne(progressInfo);

    tableModel->removeRow(iSel);
    tableModel->submit();

    selectAll();
}

void ProgressManageDialog::selectAll()
{
    qDebug() << __FUNCTION__;
    tableModel->removeRows(0, tableModel->rowCount());
    QList<Progress> list;
    if (progressService.getList(list))
    {
        for (int i = 0; i < list.size(); i++)
        {
            const Progress &progress = list.at(i);

            // 插入新行
            QList<QStandardItem *> rowItems;

            rowItems << new QStandardItem(QString::number(progress.getId()))
                     << new QStandardItem(progress.getPath())
                     << new QStandardItem(progress.getName())
                     << new QStandardItem(QString::number(progress.getVanish_up()))
                     << new QStandardItem(QString::number(progress.getCpu_use_up()))
                     << new QStandardItem(QString::number(progress.getCpu_level()))
                     << new QStandardItem(QString::number(progress.getHeartbeat_up()))
                     << new QStandardItem(progress.getHeartbeat_msg())
                     << new QStandardItem(QString::number(progress.getHeartbeat_timeout_second()))
                     << new QStandardItem(QString::number(progress.getHeartbeat_exit()))
                     << new QStandardItem(QString::number(progress.getInit_start()))
                     << new QStandardItem(QString::number(progress.getStart_sleep()))
                     << new QStandardItem(QString::number(progress.getDaemon_open()))
                     << new QStandardItem(progress.getCreateTime().toString("yyyy-MM-dd hh:mm:ss"))
                     << new QStandardItem(progress.getUpdateTime().toString("yyyy-MM-dd hh:mm:ss"));

            rowItems[0]->setData(progress.getId());
            rowItems[1]->setData(progress.getPath());
            rowItems[2]->setData(progress.getName());
            rowItems[3]->setData(progress.getVanish_up());
            rowItems[4]->setData(progress.getCpu_use_up());
            rowItems[5]->setData(progress.getCpu_level());
            rowItems[6]->setData(progress.getHeartbeat_up());
            rowItems[7]->setData(progress.getHeartbeat_msg());
            rowItems[8]->setData(progress.getHeartbeat_timeout_second());
            rowItems[9]->setData(progress.getHeartbeat_exit());
            rowItems[10]->setData(progress.getInit_start());
            rowItems[11]->setData(progress.getStart_sleep());
            rowItems[12]->setData(progress.getDaemon_open());
            rowItems[13]->setData(progress.getCreateTime().toString("yyyy-MM-dd hh:mm:ss"));
            rowItems[14]->setData(progress.getUpdateTime().toString("yyyy-MM-dd hh:mm:ss"));

            tableModel->appendRow(rowItems);
        }
        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
        ui->tableView->resizeRowToContents(0);
    }
    else
    {
        QMessageBox::critical(this, "警告", "数据库连接异常！");
    }
}

void ProgressManageDialog::on_tableView_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu(this);
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();
    menu.addAction(QIcon(":/res/img/ico/add.ico"), QString("添加"), this, &ProgressManageDialog::addProgress);
    if (iSel > -1)
    {
        menu.addAction(QIcon(":/res/img/ico/edit.ico"), QString("修改"), this, &ProgressManageDialog::upProgress);
        menu.addAction(QIcon(":/res/img/ico/delete.ico"), QString("删除"), this, &ProgressManageDialog::delProgress);
    }
    menu.exec(QCursor::pos());
}

void ProgressManageDialog::on_addBtn_clicked()
{
    Progress progressInfo;
    ProgressSaveDialog *saveDialog = new ProgressSaveDialog(&progressInfo, this);
    saveDialog->setWindowIcon(QIcon(":/res/img/ico/add.ico"));
    if (saveDialog->exec() == QDialog::Accepted)
    {
        progressService.saveOne(progressInfo);
        selectAll();
    }
}
